<?php
/*
* Class teia\webdev\php\core\ProductCatalog
* Core class for catalog of products
*
* @author Bernardo Quibiana
* @date created: 20111204
*
* @date modified: 20111204
* @modified: Bernardo Quibiana
*/

// namespace declaration: Abstract Data Types
namespace teia\webdev\php\core;

require_once 'Catalog.inc';
require_once 'HtmlUtil.inc';

class ProductCatalog extends Catalog {
	//private $page_size = 10;
	static private $filter_form = 'prod_filter_form';
	static private $neutral_option = "\t-\t";
	
	public function __construct($id, $name, $var_array, $filter, $limit=120) {
		/*
		 * prod_id, prod_name, prod_type, prod_subtype, prod_brand,
		 * prod_gender, prod_descr, prod_price, prod_events,
		 */
		//$fields = array('prod_id', 'prod_name', 'prod_type', 'prod_subtype', 'prod_brand', 'prod_gender', 'prod_descr', 'prod_price', 'prod_events');
		$limit = FunctionPool::clamp($limit, 0, 120);
		$items = Product::db_factory(null,$filter, $limit, 'ecom_prod');
		$count = count($items);
		//echo "ProductCatalog: size = $count<br />\n";
		parent::__construct($id, $name, $var_array, $items, 3, 2, $filter);
		//$this->page_size = $page_size;
	}
	
	/*
	 * Instance methods
	 */
	
	public function get_all_values($prod_field, $sort = 1) {
		$all_values = array();
		foreach ($this->items as $prod) {
			$value = $prod->get($prod_field);
			array_push($all_values, $value);
		}
		sort($all_values);
		return array_unique($all_values);
	}
	
	public function get_all_brands() {
		return $this->get_all_values('prod_brand');
	}
	
	public function get_all_prices() {
		return $this->get_all_values('prod_price');
	}
	
	public function get_all_styles() {
		return $this->get_all_values('prod_type');
	}
	
	public function get_all_prod_values($item_field, $sort = 1) {
		$all_values = array();
		foreach ($this->items as $prod) {
			$prod_values = $prod->get_all_values($item_field);
			$all_values = array_merge($all_values, $prod_values);
		}
		sort($all_values);
		return array_unique($all_values);
	}
	
	public function get_all_sizes() {
		return $this->get_all_prod_values('item_size');
	}
	
	public function get_all_colors() {
		return $this->get_all_prod_values('item_color');
	}
	
	public function get_all_slides() {
		$all_values = array();
		foreach ($this->items as $prod) {
			$value = $prod->generate_slide();
			array_push($all_values, $value);
		}
		//echo FunctionPool::assoc_to_str($all_values, "<br />\n");
		return $all_values;
	}
	
	public function get_all_slide_imgs() {
		$all_values = array();
		foreach ($this->items as $prod) {
			$img = null; //$prod->generate_slide();
			$txt = $prod->get_prod_name();
			$all_values[$txt] = $img;
		}
		//echo FunctionPool::assoc_to_str($all_values, "<br />\n");
		return $all_values;
	}
	/*
	 * IPaginationDataSource
	 */
	public function get_item_content($it_num, $page_num) {
		$prods = array_values($this->items);
		$prod = $prods[$it_num];
		//echo "product: " . $prod->get_prod_id() . "\n";
		$content = $prod->generate_thumbnail();
		return $content;
	}	
	
	public function get_page_content($page_num) {
		$content = '';
		$prods = array_values($this->items);
		$page_size = $this->get_page_size();
		$num_items = $this->get_num_items();
		
		$offset = $page_num * $page_size;
		$limit = $offset + $page_size;
		$limit = $limit < $num_items ? $limit : $num_items;
		for ($i = $offset; $i < $limit; $i++) {
			$prod = $prods[$i];
			$content .= $prod->generate_thumbnail();
		}
		
		return $content;
	}
	
	/*
	 * Code generation 
	 */
	
	public function generate_filter_form($submit_txt='Filter', $reset_txt='Reset', $method='POST', $action=null) {
		if (is_null($action)) $action = FunctionPool::get_php_self();
		$form_name = self::$filter_form;
		if (isset($_REQUEST['form_name']) && $_REQUEST['form_name'] == self::$filter_form) {
			$form_src = $_REQUEST['form_src'];
		} else {
			$form_src = FunctionPool::get_request_url();
		}
		
		$onreset = "window.location='$form_src';";
		$content = "<form method=\"$method\" action=\"$action\" onreset=\"$onreset\">\n";
		$content .= "\t<input type=\"hidden\" name=\"form_name\" value=\"$form_name\" />\n";
		$content .= "\t<input type=\"hidden\" name=\"form_src\" value=\"$form_src\" />\n";
		$content .= $this->generate_dropdown_brands();
		$content .= $this->generate_dropdown_styles();
		$content .= $this->generate_dropdown_sizes();
		$content .= $this->generate_dropdown_prices();
		$content .= $this->generate_dropdown_colors();
		$content .= "\t<br />\n";
		$content .= "\t<input type=\"reset\" name=\"reset\" value=\"$reset_txt\" />\n";
		$content .= "\t<input type=\"submit\" name=\"submit\" value=\"$submit_txt\" />\n";
		$content .= "</form>\n";
		
		return $content;
	}
	
	/**
	 * Process a filtering form and generate a filter array based on the input values
	 * @return array
	 */
	static function process_filter_form() {
		$filter = array('prod_filter' => array(), 'item_filter' => array());
		
		if (isset($_REQUEST['form_name']) && $_REQUEST['form_name'] == self::$filter_form) {
			// product attributes
			$brand = $_REQUEST['filter_brand'];
			$style = $_REQUEST['filter_style'];
			$price = $_REQUEST['filter_price'];
			if ($brand !== self::$neutral_option)	$filter['prod_filter']['prod_brand'] = $brand;
			if ($style !== self::$neutral_option)	$filter['prod_filter']['prod_type'] = $style;
			if ($price !== self::$neutral_option)	$filter['prod_filter']['prod_price'] = $price;
			// item attributes
			$size = $_REQUEST['filter_size'];
			$color = $_REQUEST['filter_color'];
			if ($size !== self::$neutral_option)	$filter['item_filter']['item_size'] = $size;
			if ($color !== self::$neutral_option)	$filter['item_filter']['item_color'] = $color;
			
			//echo "<hr /> $filter_brand <hr />";
		}
		return $filter;
	}
	
	protected function generate_dropdown($title, $name, $values, $selected) {
		$num_values = count($values);
		if ($num_values > 1) {
			// add a neutral option when there is more than one option
			$values = array_merge(array(self::$neutral_option), $values);
		}
		
		$options = FunctionPool::array_to_assoc($values, false);
		
		$content = "<h3 class=\"category_menu\">$title</h3>\n";
		$content .= "<div class=\"drop_menu\">\n";
		$content .= HtmlUtil::generate_select($options, $selected, null, $name);
		$content .= "</div>\n";	
		
		return $content;
	}
	
	public function generate_dropdown_brands($selected=null) {
		$values = $this->get_all_brands();
		return $this->generate_dropdown('Marca', 'filter_brand', $values, $selected);
	}
	
	public function generate_dropdown_prices($selected=null) {
		$values = $this->get_all_prices();
		return $this->generate_dropdown('Preço', 'filter_price', $values, $selected);
	}
	
	public function generate_dropdown_styles($selected=null) {
		$values = $this->get_all_styles();
		return $this->generate_dropdown('Estilo', 'filter_style', $values, $selected);
	}
	
	public function generate_dropdown_sizes($selected=null) {
		$values = $this->get_all_sizes();
		return $this->generate_dropdown('Tamanho', 'filter_size', $values, $selected);
	}
	
	public function generate_dropdown_colors($selected=null) {
		$values = $this->get_all_colors();
		return $this->generate_dropdown('Cor', 'filter_color', $values, $selected);
	}
}

?>